<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="750px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto">
        <el-row :gutter="10">
          <el-form-item label="库位名称" prop="wmsId">
            <tree-select
              ref="treeSelect"
              style="width: 100%"
              :props="{
                value: 'id',
                label: 'wmsName',
                children: 'children'
              }"
              :options="wmsListAll"
              :accordion="true"
              :disabled="false"
              :value="ruleForm.wmsId"
              search-key="wmsName"
              @getNode="getValue($event)"
            />
          </el-form-item>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="上期结存数量" prop="previousBalanceNum">
              <el-input-number v-model="ruleForm.previousBalanceNum" style="width: 100%" :precision="0" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上期结存金额" prop="previousBalancePrice">
              <el-input-number v-model="ruleForm.previousBalancePrice" style="width: 100%" :precision="0" :min="0" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="本期收入数量" prop="currentIncomeNum">
              <el-input-number v-model="ruleForm.currentIncomeNum" style="width: 100%" :precision="0" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="本期收入金额" prop="currentIncomePrice">
              <el-input-number v-model="ruleForm.currentIncomePrice" style="width: 100%" :precision="0" :min="0" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="本期发出数量" prop="currentIssueNum">
              <el-input-number v-model="ruleForm.currentIssueNum" style="width: 100%" :precision="0" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="本期发出金额" prop="currentIssuePrice">
              <el-input-number v-model="ruleForm.currentIssuePrice" style="width: 100%" :precision="0" :min="0" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="本期结存数量" prop="currentBalanceNum">
              <el-input-number v-model="ruleForm.currentBalanceNum" style="width: 100%" :precision="0" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="本期结存金额" prop="currentBalancePrice">
              <el-input-number v-model="ruleForm.currentBalancePrice" style="width: 100%" :precision="0" :min="0" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
    <!--     <span slot="footer">
      <el-button type="info" @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    </span> -->
  </el-dialog>
</template>
<script>
import TreeSelect from '@/components/TreeSelect'
export default {
  components: { TreeSelect },
  props: {
    materialList: {
      type: Array,
      default: () => {
        return {}
      }
    },
    userAllList: {
      type: Array,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      title: '',
      ruleForm: {
        wmsId: '',
        previousBalanceNum: '',
        previousBalancePrice: '',
        currentIncomeNum: '',
        currentIncomePrice: '',
        currentIssueNum: '',
        currentIssuePrice: '',
        currentBalanceNum: '',
        currentBalancePrice: ''
      },
      wmsListAll: [],
      rules: {
        wmsId: [{ required: true, message: '库位名称不能为空！', trigger: 'change' }]
      },
      dialogVisible: false
    }
  },
  watch: {},
  mounted() {
    this.getMaterialClassify()
  },
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
      console.log(this.ruleForm)
      this.dialogVisible = true
    },
    /**
     * @description: 获取库房库位
     * @return {*}
     */
    async getMaterialClassify() {
      const res = await this.$axios.get('wms/list')
      if (res?.data.code === 200) {
        this.wmsListAll = res.data.data
      }
    },
    getValue(value) {
      if (value) {
        this.ruleForm.wmsId = value.id
      } else {
        this.ruleForm.wmsId = ''
      }
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const res = await this.$axios.post('/wms/statistics/summary/save', this.ruleForm)
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = Object.assign({}, this.$options.data.call(this).ruleForm)
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
